'Imports Lucene.SearchProvider
Imports Aricie.DNN.Modules.LuceneSearch.Entities
Imports System.Xml.Serialization
Imports DotNetNuke.Common.Utilities
Imports System.IO

Namespace Aricie.DNN.Modules.LuceneSearch.Entities
    Public Class SearchAttributesInfo
        Public Enum ChangeStates
            UNCHANGED = 0
            ADD = 1
            EDIT = 2
            DELETE = 3
        End Enum

        Private _attributeValues As ArrayList
        Private _attributeName As String
        Private _changeState As ChangeStates = ChangeStates.UNCHANGED

        Public Sub New()
        End Sub

        Public Sub New(ByVal moduleID As Integer, ByVal attributeName As String)
            '_moduleID = moduleID
            _attributeName = attributeName
        End Sub

   

        Public Property AttributeName() As String
            Get
                Return _attributeName
            End Get
            Set(ByVal Value As String)
                _attributeName = Value
            End Set
        End Property

        Public Property AttributeValue() As ArrayList
            Get
                Return _attributeValues
            End Get
            Set(ByVal value As ArrayList)
                _attributeValues = value
            End Set
        End Property

        Public Property ChangeState() As ChangeStates
            Get
                Return _changeState
            End Get
            Set(ByVal Value As ChangeStates)
                _changeState = Value
            End Set
        End Property
    End Class
End Namespace

Namespace Aricie.DNN.Modules.LuceneSearch.Business
    Public Class SearchAttributeController
        Public Shared Function GetAttribute(ByVal moduleSettings As Hashtable) As ArrayList
            Dim toreturn As New ArrayList
            '   toreturn = DeserializeArrayList(moduleSettings("Attributes").ToString)

            Return toreturn
        End Function

        Public Shared Function SerializeArrayList(ByVal attributes As ArrayList) As String
            Dim toReturn As String = ""
            If Not attributes Is Nothing And attributes.Count > 0 Then


                Dim objStringBuilder As New StringBuilder()
                Dim myXmlSerializer As XmlSerializer = CType(DataCache.GetCache("XmlSearchSerializer"), XmlSerializer)
                If (myXmlSerializer Is Nothing) Then
                    Dim extraTypes(0) As Type
                    extraTypes(0) = New SearchAttributesInfo().GetType
                    myXmlSerializer = New XmlSerializer(attributes.GetType, extraTypes)
                    DataCache.SetCache("XmlSearchSerializer", myXmlSerializer)
                End If


                Dim objTextWriter As New StringWriter(objStringBuilder)
                myXmlSerializer.Serialize(objTextWriter, attributes)
                toReturn = objStringBuilder.ToString()
            End If
            Return toReturn
        End Function

        Public Shared Function DeserializeArrayList(ByVal attributes As String) As ArrayList
            Dim toReturn As New ArrayList
            If Not attributes Is Nothing Then
                Dim extraTypes(0) As Type
                extraTypes(0) = New SearchAttributesInfo().GetType

                Dim myXmlSerializer As XmlSerializer = CType(DataCache.GetCache("XmlSearchSerializer"), XmlSerializer)
                If (myXmlSerializer Is Nothing) Then

                    myXmlSerializer = New XmlSerializer(New ArrayList().GetType, extraTypes)
                    DataCache.SetCache("XmlSearchSerializer", myXmlSerializer)
                End If

                toReturn = DirectCast(myXmlSerializer.Deserialize(New StringReader(attributes)), ArrayList)
            End If
            Return toReturn
        End Function

        Public Shared Function GetAttributeFromName(ByVal attributes As ArrayList, ByVal attributeName As String) _
            As SearchAttributesInfo
            Dim toReturn As New SearchAttributesInfo
            For Each myAtt As SearchAttributesInfo In attributes
                If myAtt.AttributeName = attributeName Then
                    toReturn = myAtt
                    Exit For
                End If
            Next

            Return toReturn
        End Function

        Public Shared Function RemoveAttribute(ByVal attributes As ArrayList, ByVal attribute As SearchAttributesInfo) _
            As ArrayList
            Dim toReturn As New ArrayList
            toReturn = attributes
            toReturn.Remove(attribute)
            Return toReturn
        End Function
    End Class
End Namespace
